import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:rlstyles/Tool/Tool.dart';
import 'package:rlstyles/main.dart';

// ignore: must_be_immutable
class CacheImageView extends StatelessWidget {
  CacheImageView(
      {Key? key,
      this.styles,
      this.url = '',
      this.className,
      this.width,
      this.height,
      this.children = const [],
      this.onClick})
      : super(key: key) {
    mStyles = StylesMap.formMap(mergeStyle(this.styles));
  }
  final dynamic width;
  final dynamic height;
  final dynamic styles;
  // final BoxFit backgroundSize;
  Styles mStyles = Styles();
  final dynamic url;
  final String? className;
  final List<Widget> children;
  final GestureTapCallback? onClick;
  BoxFit getImageFit() {
    return mStyles.backgroundSize;
  }

  Widget loadingBuilder(
    BuildContext context,
    Widget child,
    ImageChunkEvent? loadingProgress,
  ) {
    return Frame(
      children: [],
    );
  }

  Widget errorBuilder(
    BuildContext context,
    Object error,
    StackTrace? stackTrace,
  ) {
    return Frame(
      children: [],
    );
  }

  renderImage() {
    try {
      if (url != '') {
        if (url is File) {
          return Image.file(
            url as File,
            fit: getImageFit(),
            width: getSize(size: width ?? mStyles.width, defValue: null),
            height: getSize(size: height ?? mStyles.height, defValue: null),
          );
        } else if (url.indexOf('http') != -1) {
          return CachedNetworkImage(
            imageUrl: url,
            fit: getImageFit(),
            width: getSize(size: width ?? mStyles.width, defValue: null),
            height: getSize(size: height ?? mStyles.height, defValue: null),
            // loadingBuilder: loadingBuilder,
            // errorBuilder: errorBuilder,
          );
        } else {
          return Image.asset(
            url,
            fit: getImageFit(),
            width: getSize(size: width ?? mStyles.width, defValue: null),
            height: getSize(size: height ?? mStyles.height, defValue: null),
          );
        }
      } else {
        return Container();
      }
    } catch (e) {
      print('图片加载出错');
      return Container();
    }
  }

  setStyle(dynamic newStyles) {}

  @override
  Widget build(BuildContext context) {
    final mStyle = mergeStyle(styles);
    mStyle.removeWhere((key, value) {
      return [
        'margin',
        'width',
        'height',
        'flex',
        'marginLeft',
        'marginRight',
        'marginTop',
        'marginBottom'
      ].contains(key);
    });
    return Frame(
        styles: [
          FL_Width(size: mStyles.width),
          FL_Height(size: mStyles.height),
          FL_Flex(size: mStyles.flex),
          FL_Margin(size: mStyles.margin),
          FL_MarginLeft(size: mStyles.marginLeft),
          FL_MarginRight(size: mStyles.marginRight),
          FL_MarginTop(size: mStyles.marginTop),
          FL_MarginBottom(size: mStyles.marginBottom)
        ],
        onClick: onClick,
        children: [
          ClipRRect(
              borderRadius: getBorderRadius(mStyles) ??
                  BorderRadius.all(Radius.circular(0)),
              child: renderImage()),
          children.isNotEmpty
              ? Positioned(
                  left: 0,
                  top: 0,
                  right: 0,
                  bottom: 0,
                  child: Frame(
                    children: children,
                    styles: [mergeStyle(mStyle), FL_WFull, FL_HFull],
                  ))
              : Container()
        ]);
  }
}
